#!/usr/bin/env python3
# coding=utf8

# Create a mapping file by combining the three free Font Awesome release
# fonts and tries to keep the codepoints for old version 4.2 icons.
# Newer icons are used to fill gaps and afterwards fill the new
# range in Nerd Fonts of ED00-EFFF.
#
# Range A is F000 - F2FF, the 'original' codepoint range of FA in Nerd Fonts
# Range B is F300 - F8FF, icons added to FA after 4.2
# Range C is E000 - EFFF, more icons added to FA after 4.2

# PREREQUISITES: Download Font Awesome release
# $ curl -OL https://github.com/FortAwesome/Font-Awesome/releases/download/6.5.1/fontawesome-free-6.5.1-desktop.zip
# $ unzip fontawesome-free-6.5.1-desktop.zip
# $ cd fontawesome-free-6.5.1-desktop/otfs
# $ fontforge ../../remix > ../../remix_mapping
#
# We do not use the font file generated by this script

import fontforge, os, sys

def find_destination(codepoint, font):
    global swap_codes
    for change in swap_codes:
        if codepoint not in change:
            continue
        if codepoint == change[0]:
            codepoint = change[1]
        else:
            codepoint = change[0]
        break

    if codepoint >= 0xF000 and codepoint < 0xF300:
        # Keep codepoints in legacy region 'Region A'
        return codepoint
    if codepoint < 0xF000:
        # Do not include any from 'Region C'
        return None
    # Fill gaps with the remaing icons (i.e. 'Region B')
    # That will target first the original FA codepoint range in NF (F000-F2FF)
    # and if that is full use the additional range ED00-EFFF
    # The subrange 0xEE00 - 0xEE0B is reserved for Fira Code progress icons
    for point in [ *range(0xF000, 0xF300), *range(0xED00, 0xEE00), *range(0xEE0C, 0xF000) ]:
        if point not in font:
            return point
    print("No space found - abort")
    sys.exit(1)


class Sources:
    def __init__(self):
        self.regul = fontforge.open('Font Awesome 6 Free-Regular-400.otf')
        self.solid = fontforge.open('Font Awesome 6 Free-Solid-900.otf')
        self.brand = fontforge.open('Font Awesome 6 Brands-Regular-400.otf')
        self.regul.encoding = 'UnicodeFull'
        self.solid.encoding = 'UnicodeFull'
        self.brand.encoding = 'UnicodeFull'

sources = Sources()
compo = fontforge.font()
compo.encoding = 'UnicodeFull'

names = {}

count0 = 0
count1 = 0
count2 = 0
count3 = 0
count4 = 0

# To solve some new names and keep old naming intact
renames = {
    0xF003: 'envelope_o',
    0xF006: 'star_o',
    0xF046: 'check_square_o',
    0xF057: 'remove_sign',
    0xF058: 'ok_sign',
    0xF087: 'thumbs_o_up',
    0xF088: 'thumbs_o_down',
    0xF016: 'file_o',
    0xF01D: 'play_circle_o',
    0xF045: 'share_square_o',
    0xF087: 'thumbs_o_up',
    0xF088: 'thumbs_o_down',
    0xF08A: 'heard_o',
    0xF08C: 'linkedin_square',
    0xF096: 'square_o',
    0xF097: 'bookmark_o',
    0xF0A2: 'bell_o',
    0xF0D5: 'google_plus',
    0xF0E5: 'comment_o',
    0xF0E6: 'comments_o',
    0xF0F6: 'file_text_o',
    0xF0F7: 'building_o',
    0xF10C: 'circle_o',
    0xF114: 'folder_o',
    0xF115: 'folder_open_o',
    0xF11D: 'flag_o',
    0xF123: 'star_half_o',
    0xF133: 'calendar_o',
    0xF147: 'minus_square_o',
    0xF196: 'plus_square_o',
    0xF1D9: 'paper_plane_o',
    0xF1DB: 'circle_thin',
    0xF1F7: 'bell_slash_o',
    0xF224: 'transgender',
    0xF225: 'transgender_alt',
    0xF24A: 'sticky_note_o',
    0xF250: 'hourglass_o',
    0xF278: 'map_o',
    0xF283: 'credit_card_alt',
    0xF28C: 'pause_circle_o',
    0xF28E: 'stop_circle_o',
    0xF29C: 'question_circle_o',
    0xF2B3: 'google_plus_circle',
    0xF2B7: 'envelope_open_o',
    0xF2BA: 'address_book_o',
    0xF2BC: 'address_card_o',
    0xF2BE: 'user_circle_o',
    0xF2C0: 'user_o',
    0xF2C3: 'id_card_o',
    0xf328: 'clipboard_alt',
    0xF363: 'repeat_alt',
    0xF491: 'thermometer_alt',
    0xF594: 'hotel_building',
}

# If Regular and Solid have a specific icon we prefer Regular, except for these:
prefer_solid = {
    0xF004, 0xF007, 0xF005, 0xF024, 0xF02E,
    0xF057, 0xF058, 0xF059, 0xF075, 0xF07B,
    0xF07C, 0xF086, 0xF089, 0xF0C8, 0xF0EA,
    0xF0FE, 0xF146, 0xF14C, 0xF1D8, 0xF1F6,
    0xF249, 0xF27A, 0xF28B, 0xF28D, 0xF2B4,
    0xF2B6, 0xF2B9, 0xF2BB, 0xF2BD, 0xF2C2,
}

# Special handling of some few icons, see PR #1596
move_or_drop = { 0xF30B: False, 0xF30C: False, 0xF374: True,
                 0xF536: True, 0xF537: True, 0xF538: True, 0xF539: True, 0xF53A: True, 0xF53B: True, # move for progress icons
                 0xF53C: True, 0xF53D: True, 0xF53E: True, 0xF53F: True, 0xF540: True, 0xF542: True, # move for progress icons
                 0xF219: 0xF3A5, 0xF10A: 0xF3FA, 0xF10B: 0xF3CD, }
swap_codes = [ (0xF167, 0xF16A), (0xF219, 0xF3A5), (0xF10A, 0xF3FA), (0xF10B, 0xF3CD), ]

block_regular = set()

print('# Intermediate mapping file')
print('#')
print('# FA-code NF-code filename FA-name')
print('#')

# Reorder processing to accomodate for glyph shifts introduced
all_points = [ *range(0xF000, 0xF900), *range(0xE000, 0xF000) ]
for code, move in move_or_drop.items():
    if not isinstance(move, bool):
        i1 = all_points.index(code)
        i2 = all_points.index(move)
        all_points[i1] = move
        all_points[i2] = code
        continue
    all_points.remove(code)
    if move:
        all_points.append(code)

for point in all_points:
    source = None
    subset = 'none'
    if point in sources.regul and point not in block_regular and point not in prefer_solid:
        source = sources.regul
        subset = 'regular'
        # Dont add the same icon multiple times
        altuni = source[point].altuni
        if altuni:
            for i, _, _ in altuni:
                if i != 0xf1db: # Allow circle to be used twice
                    block_regular.add(i)
    elif point in sources.solid:
        source = sources.solid
        subset = 'solid'
    elif point in sources.brand:
        source = sources.brand
        subset = 'brands'
    else:
        continue

    glyphname = source[point].glyphname.replace('-', '_')
    if point in renames:
        old_glyphname = glyphname
        glyphname = renames[point]
        print('# RENAME {} to {}'.format(old_glyphname, glyphname))

    if glyphname in names:
        # Assume same glyphname means same icon
        # print('{:04X} {} dropped, (already in {:0X})'.format(point, glyphname, names[glyphname]))
        count0 += 1
        continue
    names[glyphname] = point

    source.selection.select(point)
    destpoint = find_destination(point, compo)
    if destpoint is None:
        destpoint = 0
        count1 += 1
    else:
        compo.selection.select(destpoint)
        source.copy()
        compo.paste()
        compo[destpoint].glyphname = glyphname
        compo[destpoint].manualHints = True

    if not os.path.exists('../svgs/'  + subset + '/' + source[point].glyphname + '.svg'):
        if os.path.exists('../svgs/'  + 'solid' + '/' + source[point].glyphname + '.svg'):
            # Some glyphs originate from Solid but are also in Regular
            subset = 'solid'
        else:
            print('Missing SVG "{}" - abort'.format(source[point].glyphname))
            sys.exit(1)

    print('{}{:04X} {:04X} {}/{}.svg {}'.format('' if destpoint != 0 else '# ', point, destpoint, subset, source[point].glyphname, glyphname))
    if destpoint != 0:
        if point < 0xF000:
            count4 += 1
        elif point < 0xF300:
            count2 += 1
        else:
            count3 += 1

print('# Summary')
print('# - Duplicates {}'.format(count0))
print('# - Dropped {}'.format(count1))
print('# - From original range {} (0x{:X})'.format(count2, count2))
print('# - From extended F0 range {} (0x{:X})'.format(count3, count3))
print('# - From E0 range {} (0x{:X})'.format(count4, count4))

# print('\nGenerating...')
# compo.generate('FontAwesomeNew.otf')
